/**
 * Copyright 2008 Matthew Corby-Eaglen
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package std.game.objects.exits

import org.groovymud.object.room.Exit
import org.groovymud.object.ObjectLocation
import org.groovymud.object.views.View
import org.groovymud.shell.command.CommandInterpreter
import org.groovymud.object.alive.Alive
import org.groovymud.engine.event.ScopedEvent
import org.groovymud.engine.event.system.MovementEvent
import org.groovymud.object.room.Room
import org.groovymud.object.room.Room
import org.groovymud.engine.event.messages.MessageEvent
import std.game.objects.exits.events.ArrivalEvent
import std.game.objects.GroovyMudObject
import org.groovymud.engine.event.EventScope
import org.groovymud.engine.event.HeartBeatListener
/**
 * @author corbym
 *
 */
class ExitImpl extends GroovyMudObject implements Exit{
	
	String direction;
	String arrivalDirection;
	
	ObjectLocation destination;
	
	transient View view
	
	boolean go(object, String args) {
		boolean execute = (direction == args || shortNames.contains(args));
		if(execute){
			def event = new MovementEvent(object.currentContainer, destination, object as Alive, this)		
			object.fireEvent(event);
			
			event = new MessageEvent(scope: EventScope.ROOM_SCOPE, source: object)
			event.setSourceMessage("You go ${direction}.");
			event.setScopeMessage(object.getDepartureMessage(this));
			object.fireEvent(event);
			
			mudObjectAttendant.moveToLocation(object, destination)
			
			def otherExit = object.currentContainer.getExit(arrivalDirection)			
			ArrivalEvent arriveEvent = new ArrivalEvent(scope: EventScope.ROOM_SCOPE, direction: otherExit, source:object)
			arriveEvent.setScopeMessage(object.getArrivalMessage(otherExit))
			object.fireEvent(arriveEvent)
			
			object.look()
		}
		return execute
	}
	
}
